<?PHP

// the facebook client library
require_once './appinclude.php';

?>

Invite Your Friends!
<?PHP
  // show friends
  if (count($_POST['contact_friend'])>0) {
    // send the messages out to this persons friends
	$user_details = $facebook->api_client->fql_query("SELECT first_name, last_name FROM user WHERE uid=$user");
	
	$friends_list = implode(',', $_POST['contact_friend']);
	
	$fbml_content = $user_details[0]['first_name'].' '.$user_details[0]['last_name'].' would like for you to display your Who Dey pride. You can let '.$user_details[0]['first_name'].' know by adding <fb:req-choice url="http://apps.facebook.com/hot_gallery/" label="hot gallery" /> the badge to your profile.';

    $send_email_url = $facebook->api_client->notifications_sendRequest($friends_list, 'invitation', $fbml_content,'http://googlewala.com/images/one.jpg', 1);

    if ($send_email_url!='') {
        $facebook->redirect($send_email_url . '&next=' . urlencode('?to=' . $user) . '&canvas');
        ?>
        <p>Your friends have been contacted.</p>
        <?PHP
    } else {
        ?>
        <p style="color:darkred;font-weight:bold;">Oops that didn't work :(</p>
        <p>I'm afraid Facebook has put limits on the number of times you can send your friends notifications from any application, so to let them know about this Application, please use the 'share' link below. Thanks and sorry for the trouble.</p>
        <fb:share-button class="url" href="http://apps.facebook.com/hot_gallery/">
        <meta name="title" content="hot gallery"/>
        </fb:share-button>
        <?PHP
    }

} else {
    // display the list of their friends to tell
    $friends = $facebook->api_client->fql_query("SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=$user)");

    if (count($friends)>0) {
        // just to make sure they have some friends!
        ?>
        <p>Who would you like to ask?</p>
        <form method="post" id="contact_friends_form">
        <p><a href="http://apps.facebook.com/hot_gallery/invite.php">Select All</a> | <a href="http://apps.facebook.com/hot_gallery/invite.php?unselect_all=1">Unselect All</a></p>
        <div style="height:400px;overflow:auto;">
            <table>
            <tr valign="top">
            <?PHP

            $count=1;
            $checked = "checked=\"checked\"";
            if ($_GET['unselect_all']==1) {
                $checked = "";
            }

            foreach ($friends as $friend) {
                ?>
                <td>
                <?PHP
                if ($friend['pic_square']) {
                    ?>
                    <br/>
                    <a href="http://www.facebook.com/profile.php?id=<?PHP echo $friend['uid']; ?>" target="_blank"><img src="<?PHP echo $friend['pic_square']; ?>" alt="<?PHP echo $friend['name']; ?>" title="<?PHP echo $friend['name']; ?>" /></a>
                    <?PHP
                } else {
                    ?>
                    <br/><a href="http://www.facebook.com/profile.php?id=<?PHP echo $friend['uid']; ?>" target="_blank"><img src="http://googlewala.com/images/one.jpg" alt="<?PHP echo $friend['name']; ?>" title="<?PHP echo $friend['name']; ?>" /></a>
                    <?PHP
                }
                ?>
                <br />
                <input type="checkbox" name="contact_friend[]" id="contact_friend_<?PHP echo $friend['uid']; ?>" <?PHP echo $checked; ?> value="<?PHP echo $friend['uid']; ?>" />&nbsp;<a href="http://www.facebook.com/profile.php?id=<?PHP echo $friend['uid']; ?>" target="_blank"><?PHP echo $friend['name']; ?></a>
                </td>
                <?PHP
                if ($count==5) {
                    ?>
                    </tr><tr valign="top">
                    <?PHP
                    $count=1;
                } else {
                    $count++;
                }
            }
            ?>
            </tr>
            </table>
        </div>
        <br/>
        <p>Click the button below to send your friends an email asking to add this application.</p>
        <input type="submit" class="button" name="submitbut" value="Ask my friends" />
        <input type="hidden" name="submitted" value="send_friends" />
        </form>
        <?PHP
    } else {
        ?>
        <p>Sorry you don't have any friends...</p>
        <?PHP
    }
}
?> 